<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\controller\admin\v1\system;

use app\controller\admin\Auth;
use app\services\system\SystemMenusServices;
use hdphp\services\CacheService;
use think\facade\App;

/**
 * Class：系统菜单控制器
 * 创建人：Jack
 * 创建时间：2023/11/23 14:30
 * @package app\controller\admin\v1\system
 */
class SystemMenus extends Auth
{
    /**
     * SystemMenus constructor.
     * @param App                 $app
     * @param SystemMenusServices $services
     */
    public function __construct(App $app, SystemMenusServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
        $this->request->filter(['addslashes', 'trim']);
    }

    /**
     * 注释：菜单列表
     * 创建人：Jack
     * 创建时间：2023/8/1 10:33
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['is_show', ''],
            ['keyword', '']
        ]);

        return $this->success($this->services->getList($where));
    }

    /**
     * 注释：获取菜单权限和权限标识
     * 创建人：Jack
     * 创建时间：2023/8/1 10:46
     * @return mixed
     */
    public function unique()
    {
        [$menus, $uniqueAuth] = $this->services->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
        return app('json')->success(compact('menus', 'uniqueAuth'));
    }

    /**
     * 保存菜单权限
     * @return mixed
     */
    public function save()
    {
        $data = $this->request->getMore([
            ['menu_name', ''],
            ['controller', ''],
            ['module', 'admin'],
            ['action', ''],
            ['icon', ''],
            ['params', ''],
            ['path', []],
            ['name_path', ''],
            ['menu_path', ''],
            ['component', ''],
            ['api_url', ''],
            ['methods', ''],
            ['unique_auth', ''],
            ['redirect', ''],
            ['current_active', ''],
            ['is_redirect', 0],
            ['pid', 0],
            ['sort', 0],
            ['auth_type', 0],
            ['is_access', 1],
            ['is_show', 1],
            ['is_show_path', 0],
            ['remarks', '']
        ]);
        if (!$data['menu_name']) {
            return $this->fail('请填写按钮名称');
        }
        $data['path'] = implode('/', $data['path']);
        $data['is_show'] = $data['auth_type'] == 2 ? 1 : $data['is_show'];
        $data['create_time'] = time();
        if ($this->services->save($data)) {
            CacheService::redisHandler('system_menus')->clear();
            return $this->success('添加成功');
        } else {
            return $this->fail('添加失败');
        }
    }

    /**
     * 注释：菜单权限信息
     * 创建人：Jack
     * 创建时间：2023/8/1 10:48
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        if (!$id) {
            return $this->fail('数据不存在');
        }
        return $this->success($this->services->find((int)$id));
    }

    /**
     * 注释：修改菜单
     * 创建人：Jack
     * 创建时间：2023/8/1 15:16
     * @param $id
     * @return mixed
     */
    public function update($id)
    {
        if (!$id || !($menu = $this->services->get($id))) {
            return $this->fail('数据不存在');
        }
        $data = $this->request->postMore([
            ['menu_name', ''],
            ['controller', ''],
            ['module', 'admin'],
            ['action', ''],
            ['icon', ''],
            ['params', ''],
            ['path', []],
            ['name_path', ''],
            ['menu_path', ''],
            ['component', ''],
            ['api_url', ''],
            ['methods', ''],
            ['unique_auth', ''],
            ['redirect', ''],
            ['current_active', ''],
            ['is_redirect', 0],
            ['pid', 0],
            ['sort', 0],
            ['auth_type', 0],
            ['is_access', 1],
            ['is_show', 1],
            ['is_show_path', 0],
            ['remarks', '']
        ]);
        if (!$data['menu_name']) {
            return $this->fail('请输入按钮名称');
        }
        $data['path'] = implode('/', $data['path']);
        $data['is_show'] = $data['auth_type'] == 2 ? 1 : $data['is_show'];
        $data['update_time'] = time();
        if ($this->services->update($id, $data)) {
            CacheService::redisHandler('system_menus')->clear();
            return $this->success('修改成功');
        } else
            return $this->fail('修改失败');
    }

    /**
     * 注释：设置显示和隐藏
     * 创建人：Jack
     * 创建时间：2023/8/21 17:10
     * @param $id
     * @param $is_show
     * @return mixed
     */
    public function set_show($id, $is_show)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }
        $menus = $this->services->get($id);
        if (!$menus) {
            return $this->fail('找不到相关数据');
        }
        $menus->is_show = $is_show;
        $menus->update_time = time();
        if ($menus->save()) {
            CacheService::redisHandler('system_menus')->clear();
            return $this->success('修改成功');
        } else {
            return $this->fail('修改失败');
        }
    }

    /**
     * 注释：删除菜单
     * 创建人：Jack
     * 创建时间：2023/8/1 10:49
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) {
            return $this->fail('参数错误，请重新打开');
        }
        if (!$this->services->delete((int)$id)) {
            CacheService::redisHandler('system_menus')->clear();
            return $this->fail('删除失败,请稍候再试!');
        } else {
            return $this->success('删除成功!');
        }
    }


}